package com.ruoyi.project.modules.interfaceTestLog.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.project.modules.interfaceGenerator.service.IInterfaceGeneratorService;
import com.ruoyi.project.modules.interfaceinfo.domain.InterfaceInfo;
import com.ruoyi.project.modules.interfaceinfo.service.IInterfaceInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.interfaceTestLog.domain.InterfaceTestLog;
import com.ruoyi.project.modules.interfaceTestLog.service.IInterfaceTestLogService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 接口测试日志接口Controller
 *
 * @author smallrain
 * @date 2022-06-27
 */
@RestController
@RequestMapping("/interfaceTestLog/interfaceTestLog")
@Api(tags = "接口测试日志")
public class InterfaceTestLogController extends BaseController {



    @Autowired
    private IInterfaceTestLogService interfaceTestLogService;

    @Autowired
    private IInterfaceInfoService interfaceInfoService;

    @ApiOperation("保存接口测试日志")
    @PostMapping("/saveInterfaceTestLog")
    public AjaxResult saveInterfaceTestLog(@RequestBody InterfaceTestLog interfaceTestLog) {
        this.interfaceTestLogService.saveInterfaceTestLog(interfaceTestLog);
        this.generator(Arrays.asList(interfaceTestLog.getInterfaceId()));
        return AjaxResult.success();
    }

    @ApiOperation("批量保存接口测试日志")
    @PostMapping("/batchSaveInterfaceTestLog")
    public AjaxResult batchSaveInterfaceTestLog(@RequestBody List<InterfaceTestLog> interfaceTestLogs) {
        for (InterfaceTestLog interfaceTestLog : interfaceTestLogs) {
            this.interfaceTestLogService.saveInterfaceTestLog(interfaceTestLog);
        }
        List<Long> collect = interfaceTestLogs.stream().map(InterfaceTestLog::getInterfaceId).collect(Collectors.toList());
        this.generator(collect);
        return AjaxResult.success();
    }

    @Resource
    private IInterfaceGeneratorService interfaceGeneratorService;

    @ApiOperation("批量更新接口生命周期")
    @PostMapping("/batchUpdateInterface")
    public AjaxResult batchUpdateInterface(@RequestBody List<Long> interfaceIds) {
        logger.info("batchUpdateInterface id list：{}",interfaceIds);
        interfaceTestLogService.batchUpdateInterface(interfaceIds, "50");
        List<InterfaceInfo> interfaces = interfaceInfoService.selectInterfaceInfoByIds(interfaceIds);
        interfaces = interfaces.stream().filter(i->i.getLifeCycle().equals("40")).collect(Collectors.toList());
        if(CollectionUtil.isNotEmpty(interfaces)){
            List<Long> ids = interfaces.stream().map(i->i.getId()).collect(Collectors.toList());
            logger.info("batchUpdateInterface compensate id list：{}",interfaceIds);
            interfaceTestLogService.batchUpdateInterface(ids, "50");
        }
        this.generator(interfaceIds);
        return AjaxResult.success();
    }

    private void generator(List<Long> interfaceIds) {
        this.interfaceGeneratorService.generator(interfaceIds);
    }

    @GetMapping("/getContent")
    public AjaxResult getContent(Long interfaceId) {
        InterfaceTestLog interfaceTestLog = this.interfaceTestLogService.selectInterfaceTestLogByInterfaceId(interfaceId);
        if (interfaceTestLog != null) {
            return AjaxResult.success(null, interfaceTestLog);
        }
        return AjaxResult.success(null, "");
    }
}